package com.handinfo.game;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

import com.handinfo.engine.Animation;
import com.handinfo.engine.GameManager;
import com.handinfo.engine.net.BaseConnection;
import com.handinfo.engine.net.GameProtocol;
import com.handinfo.engine.resource.ObjectAdapter;
import com.handinfo.engine.resource.ResHead;
import com.handinfo.engine.resource.ResourceManager;
import com.handinfo.util.Tools;

public class LoadingScreen
{
	public LoadingScreen()
	{

	}

	public String m_loading_msg = "";

	/** 初始化影子 */
	public void loadShadow()
	{
		String url = ObjectAdapter.DIR_ANIM.concat("heim_dk_r_1").concat(
				ObjectAdapter.EXTENSION_ANIM);// 读取角色动画资源URL
		ResHead resHead = new ResHead(ObjectAdapter.RESOURCE_TYPE_ANIM, url, ResHead.BASE_VERSION);
		GameManager.getInstance().m_shadowResList.put(resHead);
		GameManager.getInstance().m_shadowAnimation = (Animation) (ResourceManager.getInstance().loadResource(
				GameManager.getInstance().m_resource_scene, resHead));

		if (GameManager.getInstance().m_shadowAnimation != null
				&& GameManager.getInstance().m_shadowAnimation.m_refsResHead != null
				&& GameManager.getInstance().m_shadowAnimation.m_refsResHead.length > 0) {
			int length = GameManager.getInstance().m_shadowAnimation.m_refsResHead.length;
			for (int i = 0; i < length; i++) {
				GameManager.getInstance().m_shadowResList
						.put(GameManager.getInstance().m_shadowAnimation.m_refsResHead[i]);
			}
		}
		Tools.debugPrintln("LoadingScreen LoadShaow 完毕");
	}

	/**
	 * loading进度
	 * @return
	 */
	public boolean loading()
	{
		switch (m_progress) {
			case 0:// 角色信息
				if (GameManager.getInstance().m_user_screen != null) {
					GameManager.getInstance().m_user_screen.dispose();
					GameManager.getInstance().m_user_screen = null;
				}
				loadShadow();
				GameManager.getInstance().m_game_message.sendUserInformation(GameManager.getInstance().m_role.m_gameID);
				GameManager.getInstance().m_counter = 3;
				break;
			case 2:
				if (GameManager.getInstance().m_role.m_teamID != 0) {
					GameManager.getInstance().m_game_message
							.sendTeamFristLogin(GameManager.getInstance().m_role.m_teamID);
					break;
				}
				else {
					m_progress += 2;
				}
			case 4:
				GameManager.getInstance().m_game_message.sendActorItemNumOpen(ActorItem.TYPE_BAG_ROLE);
				GameManager.getInstance().m_counter = 6;
				break;
			case 6:// 角色背包数据
				GameManager.getInstance().m_game_message.sendActorItemInfos(ActorItem.TYPE_BAG_ROLE);
				GameManager.getInstance().m_counter = 10;
				break;
			case 8:// 角色装备数据
				GameManager.getInstance().m_game_message.sendActorItemInfos(ActorItem.TYPE_BAG_EQUIP);
				GameManager.getInstance().m_counter = 13;
				break;
			case 10:// 角色技能
				GameManager.getInstance().m_game_message.sendCombatSkillList();
				GameManager.getInstance().m_counter = 16;
				break;
			case 12:
				GameManager.getInstance().m_game_message.sendInitShortCut();
				GameManager.getInstance().m_counter = 19;
				break;
			case 14:// 初始化宏信息
				GameManager.getInstance().m_game_message.sendInitMarco();
				GameManager.getInstance().m_counter = 22;
				break;
			case SCENE_INFORMATION:// 场景信息
				GameManager.getInstance().m_game_message.sendSceneInformation(GameManager.getInstance().m_role.m_gameID);
				GameManager.getInstance().m_counter = 25;
				break;
			case 18:// 结束
				return true;

		}
		if (m_progress % 2 == 0) {
			m_progress++;
		}
		if (m_progress > 18) {
			m_progress = 18;
			Tools.debugPrintln("m_progress 超出!");
		}
		return false;
	}

	public final int SCENE_INFORMATION = 16;

	/**
	 * loading信息读取
	 * 
	 * @param type
	 * @param body
	 */
	public void recvMessage(int type, byte[] body)
	{
		int order = BaseConnection.s_perseOrderID;
		Tools.debugPrintln("loading = " + order);
		GameManager.getInstance().m_game_message.unlockNetwork();
		try {
			ByteArrayInputStream bais = new ByteArrayInputStream(body);
			DataInputStream dis = new DataInputStream(bais);
			switch (order) {
				case GameProtocol.SC_USER_INFORMATION:
					Tools.debugPrintln("读取人物信息");
					recvRoleInformation(dis);
					m_progress++;
					break;
				case GameProtocol.SC_SCENE_INFORMATION:
					Tools.debugPrintln("读取场景信息");
					recvSceneInformation(dis);
					m_progress++;
					break;
				case GameProtocol.CS_COMBAT_SKILL_LIST:
					Tools.debugPrintln("读取技能信息!");
					GameManager.getInstance().m_role.initCombatSkill(dis);
					m_progress++;
					break;
				case GameProtocol.CS_TASK_ROLE_LIST:
					break;
				case GameProtocol.CS_ACTORITEM_INFOS:
					Tools.debugPrintln("收到道具信息");
					GameManager.getInstance().m_role.recvActorItemsFormServer(dis);
					m_progress++;
					break;
				case GameProtocol.CS_ACTORITEM_NUM_OPEN:
					Tools.debugPrintln("收到道具格子开启数目");
					GameManager.getInstance().m_role.recvActorItemNumOpen(dis);
					m_progress++;
					break;
				case GameProtocol.CS_INIT_SHORTCUT:
					GameManager.getInstance().m_role.initShortcut(dis);
					m_progress++;
					break;
				case GameProtocol.CS_INIT_MARCO:// 初始化宏
					GameManager.getInstance().m_role.initMarcos(dis);
					m_progress++;
					break;
				case GameProtocol.SC_TEAM_TEAMLEADERCHANGE:
					GameManager.getInstance().m_role.recvTeamLeaderChange(dis);
					m_progress++;
					break;
//				case GameProtocol.S_MAIL_MAILLIST:
//					GameScene.recvMailList(dis);
//					m_progress++;
//					 break ;
			}
			dis.close();
			bais.close();
			dis = null;
			bais = null;
		}
		catch (Exception ex) {
			switch (order) {
				case GameProtocol.SC_USER_INFORMATION:
				case GameProtocol.SC_SCENE_INFORMATION:
				case GameProtocol.CS_COMBAT_SKILL_LIST:
					break;
				default:
					m_progress++;
					break;
			}
			GameManager.getInstance().m_game_message.unlockNetwork();
			Tools.debugPrintln("loading网络数据异常！！");
			Tools.debug(ex);
		}
	}

	/**
	 * 读取场景信息
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recvSceneInformation(DataInputStream dis) throws IOException
	{
		GameManager.getInstance().m_counter = 25;
		GameManager.getInstance().m_stage = dis.readUTF();// 地图ID
		GameManager.getInstance().m_map_name = dis.readUTF();// 地图名
	    GameManager.getInstance().m_areaType = dis.readByte();//地区属于区域类型 0安全 1战斗
		GameManager.getInstance().m_role.deactivate();
		GameManager.getInstance().m_role_x = dis.readShort();
		GameManager.getInstance().m_role_y = dis.readShort();
		Tools.debugPrintln("地图ID : " + GameManager.getInstance().m_stage);
		Tools.debugPrintln("地图名 : " + GameManager.getInstance().m_map_name);
		Tools.debugPrintln("坐标 x : " + GameManager.getInstance().m_role.m_posX);
		Tools.debugPrintln("坐标 y : " + GameManager.getInstance().m_role.m_posY);

		byte dir = dis.readByte();// 人物方向
		Tools.debugPrintln("人物方向 : " + dir);
		GameManager.getInstance().m_role.setAction(ActorRole.ACTION_STAND * ActorRole.DIRECT_NUM + dir, true);
		GameManager.getInstance().m_sceneResList.clean();

		int resNum = dis.readShort();
		Tools.debugPrintln("resNum="+resNum);
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < resNum; i++) {
			int type = dis.readByte();
			Tools.debugPrintln("type="+type);
			String url = dis.readUTF();
			Tools.debugPrintln("url="+url);
			int version = dis.readInt();
			buffer.delete(0, buffer.length());
			switch (type) {
				case ObjectAdapter.RESOURCE_TYPE_ANIM:
					buffer.append(ObjectAdapter.DIR_ANIM);
					buffer.append(url);
					buffer.append(ObjectAdapter.EXTENSION_ANIM);
					break;
				case ObjectAdapter.RESOURCE_TYPE_BG:
					buffer.append(ObjectAdapter.DIR_BG);
					buffer.append(url);
					buffer.append(ObjectAdapter.EXTENSION_BG);
					break;
				case ObjectAdapter.RESOURCE_TYPE_PNG:
					buffer.append(ObjectAdapter.DIR_IMG);
					buffer.append(url);
					buffer.append(ObjectAdapter.EXTENSION_IMG);
					break;
				case ObjectAdapter.RESOURCE_TYPE_SCENE:
					buffer.append(ObjectAdapter.DIR_SCENE);
					buffer.append(url);
					buffer.append(ObjectAdapter.EXTENSION_SCENE);
					break;
			}
			url = buffer.toString();
			ResHead resHead = new ResHead(type, url, version);
			GameManager.getInstance().m_sceneResList.put(resHead);
			if (type == ObjectAdapter.RESOURCE_TYPE_SCENE) {
				GameManager.getInstance().m_sceneResHead = resHead;
			}
		}
//		GameManager.getInstance().m_sceneResList.merge(GameManager.getInstance().m_roleResList);
		GameManager.getInstance().m_sceneResList.merge(GameManager.getInstance().m_shadowResList);

		Tools.debugPrintln("场景信息读取完毕!");
		if (GameManager.getInstance().m_scene != null) {
			ResourceManager.getInstance().removeResource(GameManager.getInstance().m_resource_scene,
					GameManager.getInstance().m_scene.m_resHead);
			GameManager.getInstance().m_scene = null;
		}
		GameManager.getInstance().m_counter = 30;
		ResourceManager.getInstance().updateResource(GameManager.getInstance().m_resource_scene,
				GameManager.getInstance().m_sceneResList);
		GameManager.getInstance().m_counter = 75;
		GameManager.getInstance().m_scene = (GameScene) ResourceManager.getInstance().loadResource(
				GameManager.getInstance().m_resource_scene, GameManager.getInstance().m_sceneResHead);
		GameManager.getInstance().m_counter = 90;
	}

	public boolean resOver = false;
	public static final int TARGET_RANGE_FS = 145;//法师选择范围
	public static final int TARGET_RANGE_JS = 145;//剑士选择范围
	public static final int TARGET_RANGE_DK = 145;//刀客选择范围


	/**
	 * 读取角色信息
	 * 
	 * @param dis
	 * @throws IOException
	 */
	private void recvRoleInformation(DataInputStream dis) throws IOException
	{
		byte state = dis.readByte();
		if (state == 1) {
			GameManager.getInstance().m_role.m_name = dis.readUTF();// 名字
			Tools.debugPrintln("名字 : " + GameManager.getInstance().m_role.m_name);
			GameManager.getInstance().m_role.m_sex = dis.readByte();// 性别
			Tools.debugPrintln("性别 : " + GameManager.getInstance().m_role.m_sex);
			short vocationSize = dis.readShort();// 几转
			Tools.debugPrintln("职业转数 : " + vocationSize);
			GameManager.getInstance().m_role.m_vocation = new byte[vocationSize];
			for (short i = 0; i < vocationSize; i++) {
				GameManager.getInstance().m_role.m_vocation[i] = dis.readByte();// 职业
				Tools.debugPrintln("第" + i + "职业 : " + GameManager.getInstance().m_role.m_vocation[i]);
			}
			if (GameManager.getInstance().m_role.m_vocation != null) {
				if (GameManager.getInstance().m_role.m_vocation[0] == ActorRole.VOCATION_JIAN_KE) {
					GameScene.TARGET_RANGE = TARGET_RANGE_JS;
				}
				else if (GameManager.getInstance().m_role.m_vocation[0] == ActorRole.VOCATION_WU_ZHE) {
					GameScene.TARGET_RANGE = TARGET_RANGE_DK;
				}
				else if (GameManager.getInstance().m_role.m_vocation[0] == ActorRole.VOCATION_DAO_SHI) {
					GameScene.TARGET_RANGE = TARGET_RANGE_FS;
				}
			}
			GameManager.getInstance().m_role.m_race = dis.readByte();// 种族
			Tools.debugPrintln("种族 : " + GameManager.getInstance().m_role.m_race);
			GameManager.getInstance().m_role.m_faction = dis.readByte();// 阵营
			Tools.debugPrintln("阵营 : " + GameManager.getInstance().m_role.m_faction);
			GameManager.getInstance().m_role.m_lv = dis.readInt();// 等级
			Tools.debugPrintln("等级 : " + GameManager.getInstance().m_role.m_lv);
			GameManager.getInstance().m_role.m_status = dis.readByte();//状态
			Tools.debugPrintln("状态 : " + GameManager.getInstance().m_role.m_status);
			GameManager.getInstance().m_role.m_action = dis.readInt();//行为
			Tools.debugPrintln("行为 : " + GameManager.getInstance().m_role.m_action);
			GameManager.getInstance().m_role.m_hpMax = dis.readInt();// 当前最大HP
			Tools.debugPrintln("当前最大HP : " + GameManager.getInstance().m_role.m_hpMax);
			GameManager.getInstance().m_role.m_hp = dis.readInt();// 当前HP
			Tools.debugPrintln("当前HP : " + GameManager.getInstance().m_role.m_hp);
			GameManager.getInstance().m_role.m_mpMax = dis.readInt();// 当前最大MP
			Tools.debugPrintln("当前最大MP : " + GameManager.getInstance().m_role.m_mpMax);
			GameManager.getInstance().m_role.m_mp = dis.readInt();// 当前MP
			Tools.debugPrintln("当前MP : " + GameManager.getInstance().m_role.m_mp);
			GameManager.getInstance().m_role.m_exp = dis.readInt();
			Tools.debugPrintln("当前经验 : " + GameManager.getInstance().m_role.m_exp);
			GameManager.getInstance().m_role.m_max_exp = dis.readInt();
			Tools.debugPrintln("当前总经验 : " + GameManager.getInstance().m_role.m_max_exp);
			GameManager.getInstance().m_role.m_combatSkillMaxPoint = dis.readInt();
			Tools.debugPrintln("总技能点 : " + GameManager.getInstance().m_role.m_combatSkillMaxPoint);
			GameManager.getInstance().m_role.m_money_game = dis.readLong();
			Tools.debugPrintln("游戏金币 : " + GameManager.getInstance().m_role.m_money_game);
			GameManager.getInstance().m_role.m_money_yinpiao = dis.readLong();
			Tools.debugPrintln("游戏银票 : " + GameManager.getInstance().m_role.m_money_yinpiao);
			GameManager.getInstance().m_role.m_money_zhangong  = dis.readLong();
			Tools.debugPrintln("游戏战功 : " + GameManager.getInstance().m_role.m_money_zhangong);
			GameManager.getInstance().m_role.m_money_rmb = dis.readLong();
			Tools.debugPrintln("游戏元宝 : " + GameManager.getInstance().m_role.m_money_rmb);
			//解屏1
			GameManager.getInstance().m_role.m_money_banggong = dis.readLong();
			Tools.debugPrintln("游戏帮贡 : " + GameManager.getInstance().m_role.m_money_banggong);
			
			int sex = GameManager.getInstance().m_role.m_sex;
			int race = GameManager.getInstance().m_role.m_race;
			GameManager.getInstance().m_role.m_clothhead = GameManager.getInstance().getClothHead(sex, race);

			int animCount = 3;
			GameManager.getInstance().m_role.m_anims = new Animation[animCount];
			GameManager.getInstance().m_role.m_refsResHead = new ResHead[animCount];
			GameManager.getInstance().m_role.m_position = dis.readByte();//家族职位
			Tools.debugPrintln("家族职位 : " + GameManager.getInstance().m_role.m_position);
			GameManager.getInstance().m_role.m_teamID = dis.readLong();
			Tools.debugPrintln("队伍ID : " + GameManager.getInstance().m_role.m_teamID);
			GameManager.getInstance().m_role.m_needSendPath = true;
			GameManager.getInstance().m_role.m_jinglizhi_zonggong = dis.readInt(); //总精力值
			GameManager.getInstance().m_role.m_jinglizhi_shengyu = dis.readInt(); //剩余精力值
			Tools.debugPrintln("剩余精力值: " + GameManager.getInstance().m_role.m_jinglizhi_zonggong);
			Tools.debugPrintln("剩余精力值: " + GameManager.getInstance().m_role.m_jinglizhi_shengyu);
			GameManager.getInstance().m_role.initActor();
		}
	}

	public int m_progress = 0;// 进度
	// private boolean m_first = false;//是否为第一次进入游戏
}
